﻿using EM;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValueController : ControllerBase
    {
        private readonly HallDbContext hb;
        public ValueController(HallDbContext context)
        {
            hb = context;
        }
        [HttpGet]
        public Result name()
        {
            Result result = new Result();
            var resulrData = hb.User.ToList<User>();
            result.Data = resulrData;
            result.Code = 200;
            result.Author = "2195346279@qq.com";
            return result;
        }
        /// <summary>
        /// 根据ID获取数据
        /// ResultFull 风格
        /// http://localhost:5291/api/user?Id=4387e222-ec4b-4bdc-83f0-705791bdd728
        ///  http://localhost:5291/api/user/4387e222-ec4b-4bdc-83f0-705791bdd728
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// /// Path:路径参数      
        //ToList:查询
        // return hb.User.ToList();
        //hb:数据库
        //hb.user 在数据库中找到user表
        //where select组装Sql语句
        //执行sql语句 toList() FirstOrDefault()
        //顺序 先组装 后执行
        [HttpGet("{Id}/{userName}")]
        public Result Get(Guid Id, string userName)
        {
            Result result = new Result();
            // where: 过滤行
            // select: 过滤列
            // tolist(): 筛选一个范围的数据(集合)
            // FirstOrDefault(): 筛选第一条符合条件的数据行(对象)
            // <> : 确认数据类型( 泛型 )

            User? resulrData = hb.User.Where(w => w.Id == Id).FirstOrDefault<User>();
            if (resulrData == null)
            {
                result.Code = 404;
            }
            else
            {
                result.Code = 200;
                result.Data = resulrData;
            }
            return result;
        }
    }
}
